package com.company.example.task;


import com.company.example.core.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by ywl on 2016/12/26.
 */
@Component
public class AnnotationTask {

    /**
     * cron表达式：* * * * * *（共6位，使用空格隔开，具体如下）
     * cron表达式：*(秒0-59) *(分钟0-59) *(小时0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT)
     */
    //按天统计
    @Scheduled(cron = "0 0 2 * * ?")
    public void dayStatistics() {
        Date now = new Date();
        String yearStartDate = DateUtil.formatDateTime(DateUtil.addYears(now, -1), "yyyy-MM-dd");
        String startDatetime = DateUtil.formatDateTime(DateUtil.addDays(now, -1), "yyyy-MM-dd 00:00:00");
        String endDatetime = DateUtil.formatDateTime(DateUtil.addDays(now, -1), "yyyy-MM-dd 23:59:59");
        statisticsData(now,yearStartDate,startDatetime,endDatetime,1);
    }

    //按周统计
    @Scheduled(cron = "0 30 2 ? * MON")
    public void weekStatistics() {
        Date now = new Date();
        String yearStartDate = DateUtil.formatDateTime(DateUtil.addYears(now, -1), "yyyy-MM-dd");
        String startDatetime = DateUtil.formatDateTime(DateUtil.addWeeks(now, -1), "yyyy-MM-dd 00:00:00");
        String endDatetime = DateUtil.formatDateTime(DateUtil.addDays(now, -1), "yyyy-MM-dd 23:59:59");
        statisticsData(now,yearStartDate,startDatetime,endDatetime,2);
    }

    //按月统计
    @Scheduled(cron = "0 0 3 1 * ?")
    public void monthStatistics() {
        Date now = new Date();
        String yearStartDate = DateUtil.formatDateTime(DateUtil.addYears(now, -1), "yyyy-MM-dd");
        String startDatetime = DateUtil.formatDateTime(DateUtil.addMonths(now, -1), "yyyy-MM-dd 00:00:00");
        String endDatetime = DateUtil.formatDateTime(DateUtil.addDays(now, -1), "yyyy-MM-dd 23:59:59");
        statisticsData(now,yearStartDate,startDatetime,endDatetime,3);
    }

    //按季度统计
    @Scheduled(cron = "0 30 3 1 1,4,7,10 ?")
    public void quarterStatistics() {
        Date now = new Date();
        String yearStartDate = DateUtil.formatDateTime(DateUtil.addYears(now, -1), "yyyy-MM-dd");
        String startDatetime = DateUtil.formatDateTime(DateUtil.addMonths(now, -3), "yyyy-MM-dd 00:00:00");
        String endDatetime = DateUtil.formatDateTime(DateUtil.addDays(now, -1), "yyyy-MM-dd 23:59:59");
        statisticsData(now,yearStartDate,startDatetime,endDatetime,4);
    }

    //按年统计
    @Scheduled(cron = "0 0 4 1 1 ?")
    public void yearStatistics() {
        Date now = new Date();
        String yearStartDate = DateUtil.formatDateTime(DateUtil.addYears(now, -1), "yyyy-MM-dd");
        String startDatetime = DateUtil.formatDateTime(DateUtil.addYears(now, -1), "yyyy-MM-dd 00:00:00");
        String endDatetime = DateUtil.formatDateTime(DateUtil.addDays(now, -1), "yyyy-MM-dd 23:59:59");
        statisticsData(now,yearStartDate,startDatetime,endDatetime,5);
    }

    //测试
    @Scheduled(cron = "0 * 14 * * ?")
    public void testStatistics() {
        Date now = new Date();
        String yearStartDate = DateUtil.formatDateTime(DateUtil.addYears(now, -1), "yyyy-MM-dd");
        String startDatetime = DateUtil.formatDateTime(DateUtil.addYears(now, -1), "yyyy-MM-dd 00:00:00");
        String endDatetime = DateUtil.formatDateTime(DateUtil.addDays(now, -1), "yyyy-MM-dd 23:59:59");
        statisticsData(now,yearStartDate,startDatetime,endDatetime,0);
    }

    private void statisticsData(Date date,String yearStartDate,String startDatetime,String endDatetime,int type) {
        System.out.println("--------------------------------");
        System.out.println("type:"+type);
        System.out.println("date:"+date);
        System.out.println("yearStartDate:"+yearStartDate);
        System.out.println("startDatetime:"+startDatetime);
        System.out.println("endDatetime:"+endDatetime);
        System.out.println("--------------------------------");
    }
}
